/**
 * 通用列表模块
 * 
 * @Date: 2016-1-24
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ","ajaxServ","datagridServ","sysmanMain","btnServ","bizProto"], 
	function($,msgServ,ajaxServ,datagridServ,sysmanMain,btnServ,bizProto) {

	'use strict';

	var    $dgtype,$gridtype,$dgdetail,$griddetail,$edit_window,
			_defaultConfig,_moduleConfig,_buttonConfig,_bizProto,
			$edit_window_height=700,$edit_window_width=900,
			typeQryUrl,detailQryUrl,
			moduCode ='supplySelect',
			_defaultConfig,
			select_window_id = 'win-supply-select',
	//供应商选择
		supplySelectUrl='/common/query/supplySelect.action',
		supplySelectWindow='#win-supply-select',
		supplySelectWindowWith='900',
		supplySelectWindowHeight='700',
		
		//弹窗传入参数
		openParam
    
	var	private_method = {
		init : function(_options) {
			
			_defaultConfig ={
				doc:null,
				rootId:null,				
				panelSelector:'div.well',
				subSystemCode:null,
				moduleCode:null,
				typeCode:'supplyBasetype',//分类模块代码
				typeField:'integritytype',//分类字段名
				detailCode:'supplyBaseinfo'	,
				editWindow:null,
				btnSelector:'div.list-detail',
				//默认打开已经启用的供应商
				initQueryParams:{condition:{is_used:'Y'}},
				onClickRow:null,
				onDblClickRow:null,
				typeQryUrl:null
			}
			_moduleConfig = sysmanMain.getModuConfigByName(moduCode);
			if (window.console) {
				console.info('supplySelect modules gets ready and been loaded.');
			}
			
					
			_defaultConfig = $.extend({},_defaultConfig,_options)			
			_bizProto = new bizProto(_defaultConfig);			
			_bizProto.gridHeight -= 15
			
			if(_defaultConfig.editWindow){
				$edit_window = $('#' + _defaultConfig.editWindow);			
			}
			
			_buttonConfig={
				"选择":private_method.select,
				"重置":private_method.reset,
			    "取消":private_method.cancel	
			}
					
			private_method.initElement(_bizProto.pageDoc)
		},		
		
		initElement: function (doc) {		
			
			
			$dgtype = $(doc).find("table.dg-type");
			
			if(_defaultConfig.typeQryUrl){
				typeQryUrl = _defaultConfig.typeQryUrl
			}else{
				typeQryUrl='/'+_defaultConfig.typeCode +
					'/query/getTreeList.action'
			}
			var typeParam = {
				height : _bizProto.gridHeight,
				url : _bizProto.rootPath + typeQryUrl				
			}			
			
			typeParam.onClickRow = private_method.getdetail
			
			
			if(_defaultConfig.queryParam){
				typeParam.queryParams=_defaultConfig.queryParam
			}
			$gridtype=$($dgtype).treegrid(typeParam);
			//初始化操作按钮
			
			var listTd = $(doc).find(_defaultConfig.btnSelector)
						
			$dgdetail = $(doc).find("table.dg-detail");
			if(_defaultConfig.detailQryUrl){
				detailQryUrl = _defaultConfig.detailQryUrl
			}else{
				detailQryUrl=_bizProto.rootPath +'/'+_defaultConfig.detailCode +
					'/query/getListDomain.action'
			}
			var _param = {
				height :_bizProto.gridHeight,
				url : detailQryUrl,
				toolbar:$(listTd)		
			}
			//初始化查询条件判断
			if(_defaultConfig.initQueryParams){
				_param.queryParams=_defaultConfig.initQueryParams
			}
			
			//判断是否单选
			if(openParam && openParam.singleSelect){
				_param.singleSelect = true
			}
			//初始化datagrid，加载数据
			$griddetail=$($dgdetail).datagrid(_param);
			
			private_method.bindBtnEvent();
		},
		
		searchFunction:function(value,name){
			var _param = {}
	    	_param['searchName'] = name;_param['searchValue'] = value
	    	return _param;
		},
		
		
		//绑定按钮事件
		bindBtnEvent:function(){
			// 绑定按钮事件
			var btn = $(_bizProto.pageDoc).find(_defaultConfig.btnSelector +' a.function-btn')
			//判断按钮是否存在
			if(btn.length>0){
				$(btn).unbind('click');
				$(btn).bind('click', private_method.function_btn_listenner)	
			}		
		},
		// 功能按钮监听函数
		function_btn_listenner : function(e) {

			btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

		},
		 getdetail:function (rowData){
			//默认打开已经启用的供应商
		 	var _param = {is_used:'Y'}
		 	_param[_defaultConfig.typeField]=rowData.id
		 	$dgdetail.datagrid('load',{condition:_param})			 
		},
		// 新增函数
		select : function() {
			
			var records = $($dgdetail).datagrid('getSelections')
			if(!records ||　records.length<=0){
				msgServ.Msgslide('请选择供应商');
				return
			}
			
			var ids,names;
				//var ids,names
			for(var i=0;i<records.length;i++){
				ids = !ids?records[i].id:ids+','+records[i].id
				names = !names?records[i].sup_name:names+','+records[i].sup_name;				
			}
			_moduleConfig = sysmanMain.getModuConf(moduCode);
			var _tmpId,_tmpValue
			if(!openParam){
				if(_moduleConfig && _moduleConfig.callback_param){
					_tmpId =_moduleConfig.callback_param.callback_id				
					_tmpValue=_moduleConfig.callback_param.callback_value
				}else{
					_tmpId =$($edit_window).attr('callback-id')
					_tmpValue=$($edit_window).attr('callback-value')
				}
				
				$('#'+_tmpId).val(ids)
	//			$('#'+_tmpValue).textbox('setText',selectedRecord.sup_name)
				$('#'+_tmpValue).textbox('setValue',names)
			}else{
				_tmpId = $( ' #'+openParam.id);
				_tmpValue=$(' #'+ openParam.value);
				
				_tmpId.val(ids)
	//			$('#'+_tmpValue).textbox('setText',selectedRecord.sup_name)
				_tmpValue.textbox('setValue',names)
			}
			$('#'+select_window_id).window('close')
		},
		// 编辑,flag 为form按钮可见性标志
		reset : function() {
			$gridtype.treegrid('load')
			$griddetail.datagrid('load')
								
		},
		
		
		// 删除
		cancel : function() {
			
			$('#' + select_window_id).window('close');
//			$edit_window.window('close')
		}
		,
		closePage:function(){
			openParam = null;
		}
	}
	
	var public_method = {
		/**
		 * @param _options.doc	页面根节点
		 * @param _options.docroot	当前业务页面根节点
		 */
		init:function(_options){
			
			private_method.init(_options)
		},
		
		/**
		 * 脚本弹出供应商选择框
		 * @param _options.window_id	回调窗口对象id
		 * @param _options.id	 		回调id字段对象
		 * @param _options.value 		回调name字段对象
		 */
		openPage:function(_options){
			
			if(_options){
				
				//缓存传入参数
				openParam = _options;
				var _param = {
					window:$(supplySelectWindow),
					href:'/'+sysmanMain.getRootPath()+supplySelectUrl,
					width:supplySelectWindowWith,
					height:supplySelectWindowHeight,
					flag:false,
					existCheckElement:'#div-supply-select',
					onClose:private_method.closePage}
					
				datagridServ.windowEdit(_param);
			}
		}
	}	
	return public_method;
});